package com.tucc.hello.leetcode.no1_400.no1_200.no61_80.no66;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] digits = {1, 9, 9, 9};
        int[] result = solution.plusOne(digits);
        for (int i : result) {
            System.out.print(i);
        }
        System.out.println();
    }

    public int[] plusOne(int[] digits) {
        int flag = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            int sum = digits[i] + flag;
            if (sum == 10) {
                digits[i] = 0;
                flag = 1;
            } else {
                digits[i] = sum;
                flag = 0;
                break;
            }
        }
        if (flag == 1) {
            int[] result = new int[digits.length + 1];
            result[0] = 1;
            for (int i = 0; i < digits.length; i++) {
                result[i + 1] = digits[i];
            }
            return result;
        }
        return digits;
    }
}
